package com.stylefeng.guns.core.util;

import java.io.UnsupportedEncodingException;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

public class DesTest {
	private static final String Algorithm = "DES";
	/** 
     * 加密方法 
     *  
     * @param src 
     *            源数据的字节数组 
     * @return 
     */  
    public static byte[] encryptMode(byte[] key,byte[] src) {  
        try {  
            // 生成密钥  
            SecretKey deskey = new SecretKeySpec(key, Algorithm);  
            // 实例化Cipher  
            Cipher cipher = Cipher.getInstance(Algorithm);  
            cipher.init(Cipher.ENCRYPT_MODE, deskey);  
            return cipher.doFinal(src);  
        } catch (java.security.NoSuchAlgorithmException e1) {  
            e1.printStackTrace();  
        } catch (javax.crypto.NoSuchPaddingException e2) {  
            e2.printStackTrace();  
        } catch (java.lang.Exception e3) {  
            e3.printStackTrace();  
        }  
        return null;  
    }  
  
    /** 
     * 解密函数 
     *  
     * @param src 
     *            密文的字节数组 
     * @return 
     */  
    public static byte[] decryptMode(byte[] key,byte[] src) {  
        try {  
            SecretKey deskey = new SecretKeySpec(key, Algorithm);  
            Cipher c1 = Cipher.getInstance(Algorithm);  
            c1.init(Cipher.DECRYPT_MODE, deskey);  
            return c1.doFinal(src);  
        } catch (java.security.NoSuchAlgorithmException e1) {  
            e1.printStackTrace();  
        } catch (javax.crypto.NoSuchPaddingException e2) {  
            e2.printStackTrace();  
        } catch (java.lang.Exception e3) {  
            e3.printStackTrace();  
        }  
        return null;  
    }  
//    /** 
//     * 根据字符串生成密钥24位的字节数组 
//     *  
//     * @param keyStr 
//     * @return 
//     * @throws UnsupportedEncodingException 
//     */  
//    public static byte[] build3DesKey(String keyStr)  
//            throws UnsupportedEncodingException {  
//        byte[] key = new byte[24];  
//        byte[] temp = keyStr.getBytes("UTF-8");  
//  
//        if (key.length > temp.length) {  
//            System.arraycopy(temp, 0, key, 0, temp.length);  
//        } else {  
//            System.arraycopy(temp, 0, key, 0, key.length);  
//        }  
//        return key;  
//    }

    public static void main(String[] args) {
    	byte[] key = new byte[]{(byte)0x7B,(byte)0x1B,(byte)0x12,(byte)0x37,(byte)0xE8,(byte)0xCD,(byte)0xEA,(byte)0xE5};
		byte[] src = new byte[]{(byte)0x31,(byte)0x32,(byte)0x33,(byte)0x34,(byte)0x35,(byte)0x36,(byte)0x37,(byte)0x38};
		//2B   88    E8  4A  FB    95   D3   86
		//43, -120, -24, 74, -5, -107, -45, -122, -100, 77, -67, 6, -80, 109, -10, 27
		byte[] encryptData = encryptMode(key, src);//[43, -120, -24, 74, -5, -107, -45, -122, -100, 77, -67, 6, -80, 109, -10, 27]
		byte[] decryptData = decryptMode(key, encryptData);
		System.out.println(decryptData);
		//System.out.println("12345678".getBytes());
	}
}
